#include "gameform.h"
#include <QApplication>
#include <QDesktopWidget>

GameForm::GameForm(QWidget *parent) : QWidget(parent)
    , DesktopHeight(0)
    , WindowHeight(0)
    , StayMsecond(5000)
{
    ui.setupUi(this);

    //无边框样式
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    //设置窗口背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    ShowTimer = new QTimer(this);
    StayTimer = new QTimer(this);
    CloseTimer = new QTimer(this);

    QObject::connect(ShowTimer, SIGNAL(timeout()), this, SLOT(OnShowMove()));
    QObject::connect(StayTimer, SIGNAL(timeout()), this, SLOT(OnShowStay()));
    QObject::connect(CloseTimer, SIGNAL(timeout()), this, SLOT(OnClose()));
}

GameForm::~GameForm() {}

void GameForm::NowShow(QString Text, int Msecond)
{
    StayMsecond = Msecond;

    ui.MLab->setText(Text);

    QDesktopWidget* DWidget = QApplication::desktop();
    QRect Rect = DWidget->availableGeometry();
    DesktopHeight = DWidget->height();
    WindowHeight = Rect.height();

    DoPoint.setX(Rect.width() - width());
    DoPoint.setY(Rect.height() - height());

    move(DoPoint.x(), DoPoint.y());

    ShowTimer->start(4);
}

void GameForm::OnShowMove()
{
    show();

    DesktopHeight--;
    move(DoPoint.x(), DesktopHeight);
    if (DesktopHeight <= DoPoint.y())
    {
        ShowTimer->stop();
        StayTimer->start(StayMsecond);
    }
}

void GameForm::OnShowStay()
{
    StayTimer->stop();
    CloseTimer->start(4);
}

void GameForm::OnClose()
{
    DesktopHeight++;
    move(DoPoint.x(), DesktopHeight);
    if (DesktopHeight >= WindowHeight)
    {
        CloseTimer->stop();
        this->close();
    }
}

void GameForm::closeEvent(QCloseEvent *event)
{
    DesktopHeight = 0;
    WindowHeight = 0;
    StayMsecond = 5000;
    ShowTimer->stop();
    StayTimer->stop();
    CloseTimer->stop();
    event->accept();
}
